<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Channel extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/channel/manager');
	}
	
	//Quan ly kenh
	public function manager(){
		$per_page = 20;
		$num_link = 7;
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->loadScript(base_url('js/admin/jquery.modify.js'));
		$this->loadScript(base_url('js/admin/jquery.update.status.js'));
		$this->loadScript(base_url('js/admin/jquery.move.js'));
		$this->headTitle('Quản lý kênh - Admin Panel');
		
		$currentPage = $this->input->get('page',true);
		$currentPage = (!empty($currentPage))?$currentPage:'1';
		$this->load->model('TypeChannel_Model');
		$this->load->model('Channel_Model');
		$this->loadData('listtypechannel',$this->TypeChannel_Model->getAllTypeChannel());
		$this->loadData('typechannelid', '');
		$countchannel = $this->Channel_Model->countChannel();
		$this->loadData('countchannel',$countchannel);
		$pagingConfig = $this->init_pagination('administrator/channel/manager?act=paging', $countchannel, $per_page, $num_link);
		$this->loadData('paging', $this->pagination->create_links());
		$this->loadData('listchannels', $this->Channel_Model->getChannelLimit((($currentPage-1) * $pagingConfig['per_page']), $pagingConfig['per_page']));
			
		$tcid = $this->input->get('tcid', true);
		if(!empty($tcid)){
			$this->loadData('typechannelid', $tcid);
			$this->loadData('paging', '');
			$this->loadData('listchannels', $this->Channel_Model->getChannelByTcId($tcid));
		}
		if($this->input->post()){
			if($this->form_validation->run('formsearch') == true){
				$keysearch = trim($this->input->post('keysearch', true));
				$this->loadData('textkeysearch', $keysearch);
				$this->loadData('paging','');
				$this->loadData('listchannels', $this->Channel_Model->searchChannel($keysearch));
			}

		}
		$this->loadView('administrator/channel/manager');
	
	}
	//Them kenh truyen hinh
	public function add(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$errorupload = "";
		
		$this->load->model('TypeChannel_Model');
		$listtypechannel = $this->TypeChannel_Model->getAllTypeChannel();
		$this->loadData('listtypechannel',$listtypechannel);
		$this->load->model('Channel_Model');
		$this->loadData('listcodeschannel', $this->Channel_Model->codesChannel());
		if($this->input->post()){
			if($this->form_validation->run('channel') == true){							
				$upload = $this->upload_image_process('Logo', 'uploads/channels/');
				if(isset($upload['error_upload'])){	
					$errorupload = "<p>".$upload['error_upload']."</p>";
				}else{
					$url_image = $upload['image_full_url'];
					$this->Channel_Model->insertChannel($url_image);
					redirect('administrator/channel/manager');	
				}
			}		
		}
		$this->loadData('errorupload',$errorupload);
		$this->loadStylesheet(base_url('plugins/chosen/chosen.css'));
		$this->loadScript(base_url('plugins/chosen/chosen.jquery.min.js'));
		$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
		$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
		$this->loadScript(base_url('js/jquery.customFile.js'));
		$this->loadScript(base_url('js/admin/jquery.image.js'));
		$this->headTitle('Thêm kênh - Admin Panel');
		$this->loadView('administrator/channel/add');

	}
	
	//Sua kenh truyen hinh
	public function modify(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Channel_Model');	
		$channelid = $this->input->get('channelid', true);
		$channel = $this->Channel_Model->getChannelByChannelId($channelid);
		if($channel){
			$errorupload = "";
			if($this->input->post()){
				if($this->form_validation->run('channel') == true){	
					$url_image = $this->input->post('hidden_image', true);						
					if($_FILES['Logo']['error'] != 4){
						$upload = $this->upload_image_process('Logo', 'uploads/channels/');
						if(isset($upload['error_upload'])){
							$errorupload = "<p>".$upload['error_upload']."</p>";
						}else{	
							if(file_exists($url_image)){
								unlink($url_image);
							}
							$url_image = $upload['image_full_url'];	
						}
					}
					$this->Channel_Model->updateChannel($channelid, $url_image);
					redirect('administrator/channel/manager');
				}		
			}
			$this->load->model('TypeChannel_Model');
			$listtypechannel = $this->TypeChannel_Model->getAllTypeChannel();
			$this->loadData('listtypechannel',$listtypechannel);
			$this->loadData('listcodeschannel', $this->Channel_Model->codesChannel($channel->CodesChannel));
			$this->loadData('channel', $channel);
			$this->loadData('errorupload',$errorupload);
			$this->loadStylesheet(base_url('plugins/chosen/chosen.css'));
			$this->loadScript(base_url('plugins/chosen/chosen.jquery.min.js'));
			$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
			$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
			$this->loadScript(base_url('js/jquery.customFile.js'));
			$this->loadScript(base_url('js/admin/jquery.image.js'));
			$this->headTitle('Sửa bài viết - Admin Panel');
			$this->loadView('administrator/channel/modify');
		}else{
			redirect('administrator/channel/manager');
		}
	}
	//Doi trang thai kenh
	public function change_status(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$status = $this->input->post('status', true);
			$this->load->model('Channel_Model');
			$changeStatus = $this->Channel_Model->changeStatusChannel($key, $status);
			if($changeStatus){
				$json['status'] = $changeStatus;
				$json['success'] = true;
			}else{
				$json['success'] = false;
			}
			echo json_encode($json);
		}
	}
	
	//Xoa kenh
	public function delete(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$this->load->model('Channel_Model');
			$this->load->model('ServicePackChannel_Model');
			$this->ServicePackChannel_Model->deleteSPCByChannelId($key);	
			$channel = $this->Channel_Model->getChannelByChannelId($key);			
			if($this->Channel_Model->deleteChannel($key)){
				if(file_exists($channel->Logo)){
					unlink($channel->Logo);
				}
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
	
	//Sap xep kenh
	public function move(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$move = $this->input->post('move', true);
			$this->load->model('Channel_Model');
			if($this->Channel_Model->moveChannel($key, $move)){
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
}